@extends('frontend/layout/main')
@section('content')
<div class="row">
    <div class="col-md-4">
        @if(!\Session::get('username'))
        <form method="post" id="login_form">
            <div class="form-group">
                <label for="exampleInputEmail1">用户名</label>
                <input type="text" class="form-control" id="exampleInputEmail1" name="name" placeholder="你的用户名">
                <p class="text-warning error_name login_error" style="display:none"></p>
            </div>
            <div class="form-group">
                <label for="exampleInputPassword1">密码</label>
                <input type="password" class="form-control" id="exampleInputPassword1" name="password" placeholder="你的密码">
                <p class="text-warning error_password login_error" style="display:none"></p>
            </div>
            <input type="hidden" name="_token" value="{{ csrf_token() }}">
            <button type="submit" class="btn btn-primary">登录</button>
        </form>
        @else
            {{\Session::get('username')}}
        @endif

    </div>
    <div class="col-md-8">
        right
    </div>
</div>
@endsection
@section('script')
    <script type="text/javascript">
        $('#login_form').submit(function() {
            $('p.login_error').hide().html('');

            $.ajax({
                url: "{{ url('member/login') }}",
                type: 'POST',
                data: $(this).serialize(),
                success: function(result) {
                    if(result.status == 1) {
                        layer.alert(result.msg);
                    }
                },
                dataType: 'JSON',
                error: function(data) {
                    var errors = $.parseJSON(data.responseText);

                    var error_name = errors.name ? errors.name[0] : '';
                    var error_password = errors.password ? errors.password[0] : '';

                    if(error_name) {
                        $('p.error_name').show().html(error_name);
                    }

                    if(error_password) {
                        $('p.error_password').show().html(error_password);
                    }
                }
            });

            return false;
        });
    </script>
@endsection